package com.coderlin1.controller;

import com.coderlin1.entities.Customer;
import com.coderlin1.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * <Description> <br>
 *
 * @author Coder_Lin<br>
 * @version 1.0<br>
 * @taskId: <br>
 * @createDate 2021/07/30 9:48 <br>
 * @see com.coderlin1.controller <br>
 */
@RequestMapping("/customer")
@Controller
@Slf4j
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @RequestMapping("/listCustomer")
    public String listCustomer(Model model){
        List<Customer> customers = customerService.listCustomer();
        model.addAttribute("custList",customers);

        return "customer/custList";
    }

    @RequestMapping(value = "/addCustomer",method = RequestMethod.POST)
    public String addCustomer(String custName, String custMobile, String custAddress, Integer custTicket){
        Integer integer = customerService.addCustomer(custName, custMobile, custAddress, custTicket);
        if(log.isInfoEnabled()){
            log.info("addCustomer integer:"+integer);
        }

        return "redirect:/customer/listCustomer";
    }

    @RequestMapping(value = "/searchCustomerByName",method = RequestMethod.POST)
    public String searchCustomerByName(String custName,Model model){
        List<Customer> customers = customerService.searchCustomerByName(custName);
        model.addAttribute("custList",customers);

        return "customer/custList";
    }

    @RequestMapping("/modCustomerPre/{cid}")
    public String modCustomerPre(@PathVariable("cid")Integer cid,Model model){
        Customer customer = customerService.listCustomerById(cid);
        model.addAttribute("customer",customer);

        return "customer/custMod";
    }

    @RequestMapping(value = "/modCustomer",method = RequestMethod.POST)
    public String modCustomer(Integer cid,String custName, String custMobile, String custAddress, Integer custTicket){
        Integer integer = customerService.modCustomer(cid, custName, custMobile, custAddress, custTicket);
        if(log.isInfoEnabled()){
            log.info("modCustomer integer:"+integer);
        }

        return "redirect:/customer/listCustomer";
    }

    @RequestMapping("/delCustomer/{cid}")
    public String delCustomer(@PathVariable("cid") Integer cid){
        Integer integer = customerService.delCustomer(cid);
        if(log.isInfoEnabled()){
            log.info("delCustomer integer:"+integer);
        }

        return "redirect:/customer/listCustomer";
    }


}
